package com.antis.olsl.main;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.AppRoutePath;
import com.antis.olsl.arouterlib.DataRoutePath;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.library.utils.StringUtils;
import com.antis.olsl.library.widget.MyInputSearchLayout;
import com.antis.olsl.main.adapter.FunctionMenuAdapter;
import com.antis.olsl.main.adapter.FunctionSearchMenuAdapter;
import com.antis.olsl.main.bean.FunctionMenuInfo;
import com.antis.olsl.main.bean.HomePageMenuInfo;
import com.antis.olsl.main.databinding.ActivityAllFunctionBinding;
import com.antis.olsl.main.popup.FunctionPopupWindow;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.activity
 * @ClassName: AllFunctionActivity
 * @Description: 全部功能页面
 * @Author: 羊琪
 * @CreateDate: 2020/4/1 20:04
 * @UpdateDate: 2020/4/1 20:04
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Route(path = AppRoutePath.ACTIVITY_ALL_FUNCTION)
public class AllFunctionActivity extends BaseActivity<ActivityAllFunctionBinding> implements FunctionPopupWindow.FunctionMenuClickListener, MyInputSearchLayout.TextChangedListener {

    FunctionSearchMenuAdapter searchMenuAdapter;
    FunctionMenuAdapter adapter;
    ArrayList<FunctionMenuInfo> workFunctionMenuInfoList = new ArrayList<>();
    ArrayList<FunctionMenuInfo> dataMenuInfoList = new ArrayList<>();
    ArrayList<FunctionMenuInfo> magicCubeMenuInfoList = new ArrayList<>();
    ArrayList<FunctionMenuInfo> settingMenuInfoList = new ArrayList<>();
    ArrayList<HomePageMenuInfo> functionMenuInfoList = new ArrayList<>();
    String[] workFunctionTitles = new String[]{"信息", "采购", "仓储", "订单", "收银", "运营", "财务", "人资"};
    String[] dataFunctionTitles = new String[]{"商品管理", "销售管理", "企划管理", "人资管理", "档案管理"};

    int tabSelectIndex = 0;
    LinearLayoutManager linearLayoutManager;
    boolean scrollLocal = false;
    FunctionPopupWindow popupWindow;

    @Autowired
    int selectFunctionIndex = 0;

    boolean isExpand = false;
    private CountDownTimer selectTimer = new CountDownTimer(1000, 500) {
        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            scrollLocal = false;
        }
    };
    /**
     * 目标项是否在最后一个可见项之后
     */
    private boolean mShouldScroll;

    @Override
    protected ActivityAllFunctionBinding getViewBinding() {
        return ActivityAllFunctionBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initViews() {
        ARouter.getInstance().inject(this);
        viewBinding.inputSearchLayout.myInputSearchLayout.setHintText("请输入功能名称");
        viewBinding.inputSearchLayout.myInputSearchLayout.setListener(this);

        viewBinding.rvResult.setLayoutManager(new LinearLayoutManager(this));
        searchMenuAdapter = new FunctionSearchMenuAdapter(functionMenuInfoList);
        viewBinding.rvResult.setAdapter(searchMenuAdapter);
        searchMenuAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (searchMenuAdapter.getData().get(position).getActivity() != null) {
                    // readyGo(searchMenuAdapter.getData().get(position).getActivity());
                    String path = searchMenuAdapter.getData().get(position).getPath();
                    ARouter.getInstance()
                            .build(path)
                            .navigation();
                }
            }
        });

        initRecyclerView();

        initTabLayout();

        setFunctionMenuInfoList();
//        adapter.setList(workFunctionMenuInfos);
//        initTabLayout(workFunctionMenuInfos);
        System.out.println("全部大功能数目：" + functionMenuInfoList.size());

        // EventBus.getDefault().post(new EbCloseAnim(true));
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(this);
        viewBinding.recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new FunctionMenuAdapter(workFunctionMenuInfoList);
        viewBinding.recyclerView.setAdapter(adapter);

        viewBinding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int visibleItemCount = linearLayoutManager.getChildCount(); //子数
                int totalItemCount = linearLayoutManager.getItemCount(); // item总数
                final int pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition(); //当前屏幕 首个 可见的 Item 的position
                System.out.println("当前屏幕 可见的 Item 个数:" + visibleItemCount + ",Item总共的个:" + totalItemCount + ",当前屏幕 首个 可见的 Item 的position" + pastVisibleItems);
                boolean isLast = false;
                if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {//最后一个item
                    isLast = true;
                }
                if (!scrollLocal) {
                    if (null != viewBinding.tabLayout.getTabAt(pastVisibleItems)) {
                        viewBinding.tabLayout.getTabAt(pastVisibleItems).select();
                        System.out.println("设置tab selectedIndex" + pastVisibleItems);
                    }
                }

                /*int lastCompletelyVisibleItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if(lastCompletelyVisibleItemPosition==linearLayoutManager.getItemCount()-1){//滑动到底部
                    tabLayout.getTabAt(lastCompletelyVisibleItemPosition).select();
                }*/

                if (mShouldScroll) {
                    mShouldScroll = false;
                    smoothMoveToPosition(recyclerView, tabSelectIndex);
                }
            }
        });
    }

    private void initTabLayout() {
        viewBinding.tabLayout.setSelectedTabIndicatorHeight(0);
        viewBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabSelectIndex = tab.getPosition();
                View view = tab.getCustomView();
                if (null != view) {
                    TextView textView = view.findViewById(R.id.tv_title);
                    View line = view.findViewById(R.id.bottomLine);

                    textView.setSelected(true);
                    line.setVisibility(View.VISIBLE);
                }

                System.out.println("点击TAB SELECTED INDEX:" + tabSelectIndex);

                if (!scrollLocal) {
                    scrollLocal = true;
                    selectTimer.start();

                    smoothMoveToPosition(viewBinding.recyclerView, tabSelectIndex);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view) {
                    TextView textView = view.findViewById(R.id.tv_title);
                    View line = view.findViewById(R.id.bottomLine);

                    textView.setSelected(false);
                    line.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void setFunctionMenuInfoList() {
        setWorkMenu();
        setDataMenu();
        setMagicMenu();
        setSettingMenu();
    }

    /**
     * 滑动到指定位置
     *
     * @param mRecyclerView
     * @param position
     */
    private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
        // 第一个可见位置
        int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
        // 最后一个可见位置
        int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));

        if (position < firstItem) {
            // 如果跳转位置在第一个可见位置之前，就smoothScrollToPosition可以直接跳转
            mRecyclerView.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            // 跳转位置在第一个可见项之后，最后一个可见项之前
            // smoothScrollToPosition根本不会动，此时调用smoothScrollBy来滑动到指定位置
            int movePosition = position - firstItem;
            if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
                int top = mRecyclerView.getChildAt(movePosition).getTop();
                mRecyclerView.smoothScrollBy(0, top);
            }
        } else {
            // 如果要跳转的位置在最后可见项之后，则先调用smoothScrollToPosition将要跳转的位置滚动到可见位置
            // 再通过onScrollStateChanged控制再次调用smoothMoveToPosition，执行上一个判断中的方法
            mRecyclerView.smoothScrollToPosition(position);
            tabSelectIndex = position;
            mShouldScroll = true;
        }
    }

    private void setWorkMenu() {
        for (int i = 0; i < workFunctionTitles.length; i++) {
            ArrayList<HomePageMenuInfo> menuInfoList = new ArrayList<>();
            int j = 1;
            switch (i + 1) {
                case 1:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "价格调整", R.drawable.function_bt_1_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "商品淘汰", R.drawable.function_bt_1_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "供应商淘汰", R.drawable.function_bt_1_3));
                    break;
                case 2:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "品牌管理", R.drawable.function_bt_2_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "合同管理", R.drawable.function_bt_2_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "商品管理", R.drawable.function_bt_2_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "新品引进", R.drawable.function_bt_2_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "新品审核", R.drawable.function_bt_2_5));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "采购管理", R.drawable.function_bt_2_6));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "采购审核", R.drawable.function_bt_2_7));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店退货", R.drawable.function_bt_2_8));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库退货", R.drawable.function_bt_2_9));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "报损审核", R.drawable.function_bt_2_10));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "商品配置", R.drawable.function_bt_2_11));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "库存配置查询", R.drawable.function_bt_2_12));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "供应商管理", R.drawable.function_bt_2_13));
                    break;
                case 3:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库台账", R.drawable.function_bt_3_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "入库管理", R.drawable.function_bt_3_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "出库管理", R.drawable.function_bt_3_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库退货", R.drawable.function_bt_3_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库报损", R.drawable.function_bt_3_5));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库盘点", R.drawable.function_bt_3_6));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "盘点查询", R.drawable.function_bt_3_7));
                    break;
                case 4:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "仓库调拨", R.drawable.function_bt_4_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店调拨", R.drawable.function_bt_4_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店订单", R.drawable.function_bt_4_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "生成配送单", R.drawable.function_bt_4_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "订单配送", R.drawable.function_bt_4_5));
                    break;
                case 5:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店收银", R.drawable.function_bt_5_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "交接班管理", R.drawable.function_bt_5_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "退换货", R.drawable.function_bt_5_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "会员开卡", R.drawable.function_bt_5_4));
                    break;
                case 6:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "营业结账", R.drawable.function_bt_6_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "入库验收", R.drawable.function_bt_6_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "预订单", R.drawable.function_bt_6_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "预订单审核", R.drawable.function_bt_6_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店调拨", R.drawable.function_bt_6_5));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "考勤录入", R.drawable.function_bt_6_6));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "商品退货", R.drawable.function_bt_6_7));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店盘点", R.drawable.function_bt_6_8));
//                    menuInfos.add(new HomePageMenuInfo("work_" + i + "_" + j++, "盘点查询", R.drawable.function_bt_6_9));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "备用金", R.drawable.function_bt_6_10));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店商品配置", R.drawable.function_bt_6_11));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店商品库存", R.drawable.function_bt_6_12));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "日目标分解", R.drawable.function_bt_6_13));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "月目标分解", R.drawable.function_bt_6_14));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店目标", R.drawable.function_bt_6_15));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "商品报损", R.drawable.function_bt_6_16));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "微信订单", R.drawable.function_bt_6_17));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "客户退货", R.drawable.function_bt_6_18));
                    break;
                case 7:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "凭证管理", R.drawable.function_bt_7_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "科目汇总", R.drawable.function_bt_7_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "门店入账", R.drawable.function_bt_7_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "营业款入账", R.drawable.function_bt_7_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "采购单审核", R.drawable.function_bt_7_5));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "报损单审核", R.drawable.function_bt_7_6));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "促销单审核", R.drawable.function_bt_7_7));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "盘点单审核", R.drawable.function_bt_7_8));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "退货单审核", R.drawable.function_bt_7_9));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "工资表", R.drawable.function_bt_7_10));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "付款单", R.drawable.function_bt_7_11));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "报销单审核", R.drawable.function_bt_7_12));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "费用单审核", R.drawable.function_bt_7_13));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "目标设置", R.drawable.function_bt_7_14));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "总账", R.drawable.function_bt_7_15));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "明细账", R.drawable.function_bt_7_16));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "科目余额", R.drawable.function_bt_7_17));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "初始设置", R.drawable.function_bt_7_18));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "申请报销", R.drawable.function_bt_7_19));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "申请费用", R.drawable.function_bt_7_20));
                    break;
                case 8:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "员工管理", R.drawable.function_bt_8_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "薪酬设置", R.drawable.function_bt_8_2));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "社保设置", R.drawable.function_bt_8_3));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "绩效系数", R.drawable.function_bt_8_4));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "绩效设置", R.drawable.function_bt_8_5));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "提成设置", R.drawable.function_bt_8_6));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "考勤核对", R.drawable.function_bt_8_7));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "绩效核算", R.drawable.function_bt_8_8));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "工资核算", R.drawable.function_bt_8_9));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "考勤录入", R.drawable.function_bt_8_10));
                    break;
                default:
                    break;
            }
            functionMenuInfoList.addAll(menuInfoList);
            workFunctionMenuInfoList.add(new FunctionMenuInfo(workFunctionTitles[i], menuInfoList));
        }
    }

    private void setDataMenu() {
        for (int i = 0; i < dataFunctionTitles.length; i++) {
            ArrayList<HomePageMenuInfo> menuInfoList = new ArrayList<>();
            int j = 1;
            switch (i + 1) {
                case 1:
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "库存查询", R.drawable.function_data_1_1, DataRoutePath.ACTIVITY_STOCK_QUERY));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "引进查询", R.drawable.function_data_1_2, DataRoutePath.ACTIVITY_INTRODUCTION_QUERY));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "采购查询", R.drawable.function_data_1_3, DataRoutePath.ACTIVITY_PURCHASE_QUERY));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "配送查询", R.drawable.function_data_1_4, DataRoutePath.ACTIVITY_DISTRIBUTION_QUERY));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "订单查询", R.drawable.function_data_1_5, DataRoutePath.ACTIVITY_ALLOCATE_SLIP_QUERY));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "调拨查询", R.drawable.function_data_1_6));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "退货查询", R.drawable.function_data_1_7));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "报损查询", R.drawable.function_data_1_8));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "淘汰查询", R.drawable.function_data_1_9));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "采购差异", R.drawable.function_data_1_10));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "配送差异", R.drawable.function_data_1_11));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "出库差异", R.drawable.function_data_1_12));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "订单差异", R.drawable.function_data_1_13));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "盘点查询", R.drawable.function_data_1_14));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "效期预警", R.drawable.function_data_1_15));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "滞销预警", R.drawable.function_data_1_16));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "台账查询", R.drawable.function_data_1_17));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "库存配置查询", R.drawable.function_data_1_18));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "商品库存查询", R.drawable.function_data_1_19));
                    break;
                case 2:
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "POS流水", R.drawable.function_data_2_1, DataRoutePath.ACTIVITY_POS_TURNOVER));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "销售员绩效", R.drawable.function_data_2_2));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "单品销售", R.drawable.function_data_2_3));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品类销售", R.drawable.function_data_2_4));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品牌销售", R.drawable.function_data_2_5));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "四季销售", R.drawable.function_data_2_6));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "会员销售", R.drawable.function_data_2_7));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "会员销售流水", R.drawable.function_data_2_8));
                    break;
                case 3:
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j++, "档期查询", R.drawable.function_data_3_1));
                    menuInfoList.add(new HomePageMenuInfo("work_" + i + "_" + j, "促销销售查询", R.drawable.function_data_3_2));
                    break;
                case 4:
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "提成查询", R.drawable.function_data_4_1));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "工资查询", R.drawable.function_data_4_2));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品牌提成", R.drawable.function_data_4_3));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品类提成", R.drawable.function_data_4_4));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "员工绩效", R.drawable.function_data_4_5));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "员工考勤", R.drawable.function_data_4_6));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "员工排班", R.drawable.function_data_4_7));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "备用金收支", R.drawable.function_data_4_8));
                    break;
                case 5:
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "商品档案", R.drawable.function_data_5_1));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "合同档案", R.drawable.function_data_5_2));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品牌档案", R.drawable.function_data_5_3));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "员工档案", R.drawable.function_data_5_4));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "仓库档案", R.drawable.function_data_5_5));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "门店档案", R.drawable.function_data_5_6));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "会员档案", R.drawable.function_data_5_7));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "地址档案", R.drawable.function_data_5_8));
                    menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "供应商档案", R.drawable.function_data_5_9));
                    break;
                default:
                    break;
            }
            functionMenuInfoList.addAll(menuInfoList);
            dataMenuInfoList.add(new FunctionMenuInfo(dataFunctionTitles[i], menuInfoList));
        }
    }

    private void setMagicMenu() {
        ArrayList<HomePageMenuInfo> menuInfoList = new ArrayList<>();
        int i = 0;
        int j = 0;
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "销售报表", R.drawable.function_magic_cube_1_1, MagicRoutePath.ACTIVITY_SALES_REPORT));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "三级客单", R.drawable.function_magic_cube_1_2, MagicRoutePath.ACTIVITY_GUEST_LIST));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "四季分析", R.drawable.function_magic_cube_1_3, MagicRoutePath.ACTIVITY_QUARTERLY_ANALYSIS));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "销售Top60", R.drawable.function_magic_cube_1_4, MagicRoutePath.ACTIVITY_TOP60));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "暴力分析", R.drawable.function_magic_cube_1_5, MagicRoutePath.ACTIVITY_VIOLENCE_ANALYSIS));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "五线谱", R.drawable.function_magic_cube_1_6, MagicRoutePath.ACTIVITY_STAVE));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "五级毛利", R.drawable.function_magic_cube_1_7, MagicRoutePath.ACTIVITY_LEVEL5_PROFIT));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "单品货效", R.drawable.function_magic_cube_1_8, MagicRoutePath.ACTIVITY_PRODUCT_EFFICIENCY));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品牌货效", R.drawable.function_magic_cube_1_9, MagicRoutePath.ACTIVITY_BRAND_EFFICIENCY));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "品类货效", R.drawable.function_magic_cube_1_10, MagicRoutePath.ACTIVITY_CATEGORY_EFFICIENCY));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "出库数量Top", R.drawable.function_magic_cube_1_11, MagicRoutePath.ACTIVITY_OUTBOUND_NUMBER));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "出库金额Top", R.drawable.function_magic_cube_1_12, MagicRoutePath.ACTIVITY_OUTBOUND_AMOUNT));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "出库利润Top", R.drawable.function_magic_cube_1_13, MagicRoutePath.ACTIVITY_OUTBOUND_PROFIT));
        functionMenuInfoList.addAll(menuInfoList);

        magicCubeMenuInfoList.add(new FunctionMenuInfo("魔方", menuInfoList));
    }

    private void setSettingMenu() {
        ArrayList<HomePageMenuInfo> menuInfoList = new ArrayList<>();
        int i = 0;
        int j = 0;
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "公司设置", R.drawable.function_setting_1_1));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "部门管理", R.drawable.function_setting_1_2));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "门店管理", R.drawable.function_setting_1_3));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "仓库管理", R.drawable.function_setting_1_4));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "账号管理", R.drawable.function_setting_1_5));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "角色管理", R.drawable.function_setting_1_6));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "岗位管理", R.drawable.function_setting_1_7));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "会员设置", R.drawable.function_setting_1_8));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "商品类别", R.drawable.function_setting_1_9));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "门店打印", R.drawable.function_setting_1_10));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j++, "POS小票", R.drawable.function_setting_1_11));
        menuInfoList.add(new HomePageMenuInfo("data_" + i + "_" + j, "编码/动销配置", R.drawable.function_setting_1_12));
        functionMenuInfoList.addAll(menuInfoList);

        settingMenuInfoList.add(new FunctionMenuInfo("设置", menuInfoList));
    }

    @Override
    protected void initData() throws IllegalAccessException {
        switch (selectFunctionIndex) {
            case 0:
                // setWorkMenu();
                initTabLayout(workFunctionMenuInfoList);
                adapter.setList(workFunctionMenuInfoList);
                break;
            case 1:
                // setDataMenu();
                initTabLayout(dataMenuInfoList);
                adapter.setList(dataMenuInfoList);
                break;
            case 2:
                // setMagicMenu();
                initTabLayout(magicCubeMenuInfoList);
                adapter.setList(magicCubeMenuInfoList);
                break;
            case 3:
                // setSettingMenu();
                initTabLayout(settingMenuInfoList);
                adapter.setList(settingMenuInfoList);
                break;
            default:
                break;
        }
    }

    private void initTabLayout(ArrayList<FunctionMenuInfo> functionMenuInfos) {
        if (functionMenuInfos.size() <= 1) {
            viewBinding.tabLayout.setVisibility(View.GONE);
        } else {
            viewBinding.tabLayout.setVisibility(View.VISIBLE);
            viewBinding.tabLayout.removeAllTabs();
            for (int i = 0; i < functionMenuInfos.size(); i++) {
                //获得每一个tab
                TabLayout.Tab tab = viewBinding.tabLayout.newTab();
                //给每一个tab设置view
                tab.setCustomView(R.layout.tab_item_function_title);
                View view = tab.getCustomView();
                if (null != view) {
                    TextView textView = view.findViewById(R.id.tv_title);
                    View line = view.findViewById(R.id.bottomLine);

                    textView.setText(functionMenuInfos.get(i).getMenuTitle());
                    textView.setSelected(i == tabSelectIndex);
                    line.setVisibility(i == tabSelectIndex ? View.VISIBLE : View.GONE);
                }
                viewBinding.tabLayout.addTab(tab);
            }
            viewBinding.tabLayout.setTabMode(viewBinding.tabLayout.getTabCount() > 5 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
        }
    }

    @Override
    public void onClickFunctionMenuListener(int index) {
        selectFunctionIndex = index;
        switch (index) {
            case 0:
                initTabLayout(workFunctionMenuInfoList);
                adapter.setList(workFunctionMenuInfoList);
                break;
            case 1:
                initTabLayout(dataMenuInfoList);
                adapter.setList(dataMenuInfoList);
                break;
            case 2:
                initTabLayout(magicCubeMenuInfoList);
                adapter.setList(magicCubeMenuInfoList);
                break;
            case 3:
                initTabLayout(settingMenuInfoList);
                adapter.setList(settingMenuInfoList);
                break;
            default:
                break;
        }

        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    @Override
    public void onPopupWindowDismissListener() {
        // mIvRight.setRotation(90);
        isExpand = false;
    }

    @Override
    public void onTextChangeListener(String text) {
        searchFunction(text);
    }

    /**
     * 搜索
     *
     * @param keyword
     */
    private void searchFunction(String keyword) {
        ArrayList<HomePageMenuInfo> resultMenuInfoList = new ArrayList<>();
        if (!TextUtils.isEmpty(keyword)) {
            for (int i = 0; i < functionMenuInfoList.size(); i++) {
                if (functionMenuInfoList.get(i).getMenuName().contains(keyword)) {
                    functionMenuInfoList.get(i).setResult(StringUtils.matcherSearchText(this, functionMenuInfoList.get(i).getMenuName(), keyword));
                    resultMenuInfoList.add(functionMenuInfoList.get(i));
                }
            }
        }
        if (resultMenuInfoList.size() != 0) {
            viewBinding.rvResult.setVisibility(View.VISIBLE);
            viewBinding.recyclerView.setVisibility(View.GONE);
            viewBinding.tabLayout.setVisibility(View.GONE);
            searchMenuAdapter.setList(resultMenuInfoList);
        } else {
            viewBinding.rvResult.setVisibility(View.GONE);
            viewBinding.recyclerView.setVisibility(View.VISIBLE);
            viewBinding.tabLayout.setVisibility(selectFunctionIndex == 2 || selectFunctionIndex == 3 ? View.GONE : View.VISIBLE);
        }

    }
}
